package com.wande.dataplatform.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wande.common.core.domain.R;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.bo.SystemConfigBo;
import com.wande.dataplatform.domain.vo.SystemConfigVO;
import com.wande.dataplatform.service.ISystemConfigService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统配置控制器
 *
 * @author wande
 */
@Validated
@RestController
@RequestMapping("/dataplatform/system-config")
@RequiredArgsConstructor
public class SystemConfigController extends BaseController {

    private final ISystemConfigService systemConfigService;

    /**
     * 查询系统配置列表
     */
    @SaCheckPermission("dataplatform:config:list")
    @GetMapping("/list")
    public TableDataInfo<SystemConfigVO> list(SystemConfigBo bo, PageQuery pageQuery) {
        return systemConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 根据配置类型查询配置列表
     */
    @SaCheckPermission("dataplatform:config:list")
    @GetMapping("/listByType/{configType}")
    public R<List<SystemConfigVO>> listByType(@PathVariable String configType) {
        return R.ok(systemConfigService.queryByType(configType));
    }

    /**
     * 获取系统配置详情
     */
    @SaCheckPermission("dataplatform:config:query")
    @GetMapping("/{id}")
    public R<SystemConfigVO> getInfo(@PathVariable Long id) {
        return R.ok(systemConfigService.queryById(id));
    }

    /**
     * 根据配置键获取配置值
     */
    @SaCheckPermission("dataplatform:config:query")
    @GetMapping("/value/{configKey}")
    public R<String> getConfigValue(@PathVariable String configKey) {
        return R.ok(systemConfigService.getConfigValue(configKey));
    }

    /**
     * 新增系统配置
     */
    @SaCheckPermission("dataplatform:config:add")
    @Log(title = "系统配置", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Valid @RequestBody SystemConfigBo bo) {
        return toAjax(systemConfigService.insertByBo(bo));
    }

    /**
     * 修改系统配置
     */
    @SaCheckPermission("dataplatform:config:edit")
    @Log(title = "系统配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Valid @RequestBody SystemConfigBo bo) {
        return toAjax(systemConfigService.updateByBo(bo));
    }

    /**
     * 批量更新配置
     */
    @SaCheckPermission("dataplatform:config:edit")
    @Log(title = "系统配置", businessType = BusinessType.UPDATE)
    @PutMapping("/batchUpdate")
    public R<Void> batchUpdate(@Valid @RequestBody List<SystemConfigBo> configs) {
        return toAjax(systemConfigService.batchUpdate(configs));
    }

    /**
     * 删除系统配置
     */
    @SaCheckPermission("dataplatform:config:remove")
    @Log(title = "系统配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable List<Long> ids) {
        return toAjax(systemConfigService.deleteByIds(ids));
    }

    /**
     * 验证配置
     */
    @SaCheckPermission("dataplatform:config:test")
    @PostMapping("/validate/{configType}")
    public R<Map<String, Object>> validateConfig(@PathVariable String configType) {
        return R.ok(systemConfigService.validateConfig(configType));
    }

    /**
     * 测试Doris连接
     */
    @SaCheckPermission("dataplatform:config:test")
    @PostMapping("/testDoris")
    public R<Map<String, Object>> testDorisConnection() {
        return R.ok(systemConfigService.testDorisConnection());
    }

    /**
     * 测试DolphinScheduler连接
     */
    @SaCheckPermission("dataplatform:config:test")
    @PostMapping("/testDolphinScheduler")
    public R<Map<String, Object>> testDolphinSchedulerConnection() {
        return R.ok(systemConfigService.testDolphinSchedulerConnection());
    }

    /**
     * 导出配置
     */
    @SaCheckPermission("dataplatform:config:export")
    @GetMapping("/export/{configType}")
    public R<String> exportConfig(@PathVariable String configType) {
        return R.ok(systemConfigService.exportConfig(configType));
    }

    /**
     * 导入配置
     */
    @SaCheckPermission("dataplatform:config:import")
    @Log(title = "系统配置", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public R<Void> importConfig(@RequestBody String configJson) {
        return toAjax(systemConfigService.importConfig(configJson));
    }

    /**
     * 刷新配置缓存
     */
    @SaCheckPermission("dataplatform:config:refresh")
    @Log(title = "系统配置", businessType = BusinessType.UPDATE)
    @PostMapping("/refreshCache")
    public R<Void> refreshCache() {
        systemConfigService.refreshCache();
        return R.ok();
    }
}
